#coding=utf-8
'''
Created on Aug 2, 2016

@author: wangpeng
'''
import unittest
from appium import webdriver
from selenium.webdriver.common.by import By   #用于匹配某个值是否出现，智能等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import common_setting.logging_save
import common_setting.desired_setting
import common_setting.config
import common_setting.Create_Log_Folder
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
import choose_mobile
# 测试内容
# 1.DUT进入配对模式，2.手机搜索进行配对连接，3.手机取消配对连接，4.手机再次搜索进行配对连接

class Test(unittest.TestCase):
    def setUp(self):
        os.system('taskkill /f /im adb.exe')
        common_setting.logging_save.logging.info("app测试开始，开始测试连接")
        print time.ctime(),"app测试开始，开始测试连接"
        self.save_log = common_setting.Create_Log_Folder.Create('\\basic_function\\connect_device')    #建立report保存路径
        self.desired_setting = common_setting.desired_setting.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_setting)
        time.sleep(10)

    def tearDown(self):
        self.driver.quit()
        time.sleep(15)
        common_setting.logging_save.logging.info("此项测试结束")
        print time.ctime(),'此项测试结束'


    def testName(self):
        u"""测试步骤：1.DUT进入配对模式   2.手机搜索进行配对连接  3.手机取消配对连接   4.手机再次搜索进行配对连接  预期结果：已经连接成功"""
        savenumber = 1
        self.save_pic = os.path.join(self.save_log+str(savenumber)+'.png')
        #确认名字是否确认，然后进行连接

        if choose_mobile.want_to_test_mobile == 'huawei_honor7':
            import common_setting.cell_function.test  #不同手机调用不同的配置
            time.sleep(5)

        else:
            pass

        '''
        self.driver.start_activity("com.android.settings", ".HWSettings")
        common_setting.logging_save.logging.info("app测试开始，开始检测蓝牙名字是否对应")
        print time.ctime(),'app测试开始，开始检测蓝牙名字是否对应'
        time.sleep(5)
        self.driver.find_element_by_xpath("//android.widget.TextView[@text='蓝牙']").click()
        time.sleep(5)
        before = str("//android.widget.TextView[@text= '")                                                                                                                                                                                                                
        match = common_setting.config.bluetooth_id()
        after = str("'] ")
        name_detect = before+match+after
        print name_detect
        #self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_setting)
        try:
            WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.XPATH,name_detect))) #确认是否进入到主界面                       
            common_setting.logging_save.logging.info("经过确认，可以找到此广播ID值")
            print time.ctime(),"经过确认，可以找到此广播ID值"
            time.sleep(3)
            
        except:
            self.driver.get_screenshot_as_file(self.save_pic)
            self.fail("未找到此广播ID值，测试停止")
        
        #判断当前蓝牙是处于连接状态，还是断开状态
        try:        
            WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.XPATH,"//android.widget.TextView[@text='已连接']"))) #确认是否进入到主界面                       
            common_setting.logging_save.logging.info("经过确认，蓝牙已经处于连接状态")
            print time.ctime(),"经过确认，已经连接成功"
            time.sleep(3)
            self.driver.find_element_by_xpath(name_detect).click()
            time.sleep(4)
            self.driver.find_element_by_xpath("//android.widget.Button[@text='确定']").click()
            common_setting.logging_save.logging.info("断开蓝牙操作")
            print time.ctime(),"断开蓝牙操作"
            time.sleep(10)
            
        except:
            pass
           
        ####让BT speaker进入配对状态，等待youjunpen状态反馈，放蓝牙配对命令
        ##########################################等待   
        
        
        common_setting.logging_save.logging.info("蓝牙开始配对")
        print time.ctime(),"蓝牙开始配对"
        self.driver.find_element_by_xpath(name_detect).click()    #连接设备
        time.sleep(10)
        #el = self.driver.find_element_by_xpath("//android.widget.TextView[@text='已连接']")
        #self.driver.find_element_by_xpath("self.driver.find//android.widget.TextView[@text='已连接']")

        try:
            #检测是否正确，如果有错误就报错
            self.assertIsNotNone(self.driver.find_element_by_xpath("//android.widget.TextView[@text='已连接']"))
            common_setting.logging_save.logging.info("正常检测到蓝牙连接成功")
            print time.ctime(),"正常检测到蓝牙连接成功"
        except:
            self.driver.get_screenshot_as_file(self.save_pic)
            self.fail("未检测到蓝牙处于连接状态，请确认问题所在")
        '''
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()